<template>
  <div>
    <div class="main" style="padding: 20px">
      <div style="margin: 20px">
        <h4>所属分类 ：</h4>
        <el-radio-group v-model="radio">
          <el-radio :label="0">旅游攻略</el-radio>
          <el-radio :label="1">新闻资讯</el-radio>
          <el-radio :label="2">书院趣事</el-radio>
        </el-radio-group>
      </div>
      <div style="margin: 20px">
        <h4>帖子标题 ：</h4>
        <el-input v-model="input" placeholder="请输入标题"></el-input>
      </div>
      <div style="margin: 20px">
        <h4>帖子内容 ：</h4>
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="textarea"
        >
        </el-input>
      </div>

      <div style="display: block; text-align: center; margin-top: 50px">
        <el-button style="margin-right: 100px" @click="postInput" type="primary"
          >发布</el-button
        >
        <el-button @click="$router.push('/talk')" type="danger">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import requests from '@/api/ajax';
import { mapState } from 'vuex';
export default {
  data() {
    return {
      radio: 0,
      input: '',
      textarea: '',
    };
  },
  computed: {
    ...mapState({
      userId: (state) => {
        return state.login.id;
      },
    }),
  },
  methods: {
    postInput() {
      requests
        .post('/blogs/saveBlog', {
          type: this.radio,
          userId: this.userId,
          title: this.input,
          content: this.textarea,
        })
        .then((res) => {
          if (res.code == 1) {
            this.$notify.success('帖子发布成功');
            this.input = '';
            this.textarea = '';
            this.$router.push('/talk');
          } else {
            this.$notify.warning(res.msg);
          }
        });
    },
  },
};
</script>

<style scoped>
.main {
  /* text-align: center; */
  margin: 60px auto 0;
  width: 60%;
  /* height: 500px; */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
</style>
